package com.boilerCloud.controller.user;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.user.AppUser;
import com.boilerCloud.service.user.IAppUserService;


@RestController
@RequestMapping("/front/")
public class AppUserControler {
	private static final Logger log = LoggerFactory.getLogger(AppUserControler.class);
	@Resource
	private IAppUserService service;
	
	

	@RequestMapping(value = "frontUserList")
	public ModelAndView frontUserList() {
		return new ModelAndView("/views/user/userInfo.html");
	}
	
	/**
	 * 经销商管理
	 * @return
	 */
	@RequestMapping(value = "dealerList")
	public ModelAndView dealerList() {
		return new ModelAndView("/views/user/dealerInfo.html");
	}
	
	@RequestMapping(value = "agentList")
	public ModelAndView agentList() {
		return new ModelAndView("/views/user/agentInfo.html");
	}

	/**
	 * 查询用户列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryUserList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryModelList(@ModelAttribute AppUser model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<AppUser> page = new HzlqswPage<AppUser>();
			page.setCurPage(yema);
			page.setPageSize(length);
			service.qryList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询用户列表失败");
			log.error("查询用户列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改用户信息
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("修改APP用户")
	@RequestMapping(value = "editUser", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editModel(
			@ModelAttribute  AppUser model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.edit(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改用户信息失败");
			log.error("修改用户信息异常",e);
			return rst;
		}
	}
	/**
	 * 新增用户
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增APP用户")
	@RequestMapping(value = "addUser", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addModel(
			@ModelAttribute  AppUser model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.save(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增用户信息失败");
			log.error("新增用户信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除用户
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@LogAnnotation("删除APP用户")
	@RequestMapping(value = "delUser", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delModel(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.del(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除用户失败");
			log.error("删除用户异常",e);
			return rst;
		}
	}
	

	/**
	 * 获取用户列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getAppUserList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelList(
			@ModelAttribute  AppUser model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			List<AppUser> list = service.getAppUserList(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(list);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取用户列表失败");
			log.error("获取用户列表异常",e);
			return rst;
		}
	}

	@RequestMapping(value = "getAppUserInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelInfo(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			AppUser model = service.getAppUserInfo(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取用户信息失败");
			log.error("获取用户信息异常",e);
			return rst;
		}
	}

}
